<?php
namespace Home\Controller;
use Think\Controller;
class JieController extends CommonController {

	public function index()
	{

		$id = I('get.id');

        //问题数据
        $sql ="select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$id}'";
        //关联表查询数据，问题表中t1所有数据，用户表中t2中只要昵称，需要什么取什么
        $res = M()->query($sql);  
        //实例化
        $question = current($res);

        // 增加浏览量
        $this->autoAdd('question','view_num',"+1",$id);

        //获取hot
        $this->getTopView();
        $this->getTopAnswer();


        $sql = "select t1.*,t2.nickname,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$id}'";

        $answers = M()->query($sql);

        //收藏功能
        $sql="select * from store";
        $store = M()-> query($sql);
        $store = current($store);


        $this->assign('answers',$answers);
		$this->assign('question',$question);

        $this->assign('title',$question['title']);
        $this->assign('store',$store);

		$this->display();
	}
    public function add(){
    	//获取随机一条人类验证
    	$question = D('verify')->getOneVerify();

    	$this->assign('question',$question);

    	$cates = D('category')->getCate();
    	$this->assign('cates',$cates);
        $this->display();
    }
    public function ajaxAdd()
    {

    	$verify = I('post.verify');

        // 判断人类验证是否正确
    	if($verify != $_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    		exit;
    	}

        // $this->autoAdd('user','kiss',-20);

    	$data = I('post.');  //接数据

    	$data['uid']=$_SESSION['id'];   //获取已登录的存在SESSION里的id

    	$data['create_time'] =time();   //追加时间

    	if($id = M('question')->add($data)){
    		$arr = array(
    			'error'=>0,
    			'info'=>'发布成功',
    			'url'=>U('home/jie/index',array('id'=>$id))
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'发布失败'
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	
    }

    public function uploadImage()
    {
    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Pic/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    if(!$info) {// 上传错误提示错误信息
	        echo $upload->getError();
	    }else{// 上传成功
	    	$info = current($info);
	    	$path = __ROOT__."/Pic/".$info['savepath'].$info['savename'];

	    	$arr = array(
	    		'code'=>0,
	    		'msg'=>'成功',
	    		'data'=>array(
	    			'src'=>$path,
	    			'title'=>'图片',
	    			)
	    		);
	    	echo json_encode($arr);
	    }
    }

    public function addAnswer()  //把回复的追加到answer表中
    {
        $data = I('post.');
        $data['uid']=$_SESSION['id'];
        $data['create_time'] = time();

        if(M('answer')->add($data)){
            $this->autoAdd('question','answer_num',"+1",I('post.qid'));
            $this->autoAdd('user','answer_num',"+1",$_SESSION['id']);
            //用户表和回答表中个加1
            $arr = array(
                'error'=>0,
                'info'=>'回复成功'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'回复失败'
                );
            echo json_encode($arr);
            exit;
        }
    }
    public function So()
    {
    	$q = I('get.q');
    	// var_dump($_GET);
    	Vendor('Pscws4.pscws4','','.class.php');     //引类
    	$pscws = new \PSCWS4('utf8');		//实例化该类，加\表明这个类是全局的
    	
    	$pscws->set_dict('public/dict.utf8.xdb');   // 引入词典
    	$pscws->send_text($q);

    	$tops = $pscws->get_tops(3, 'n,v');
    	//get_tops() 返回分词结果的词语按权重统计的前 N 个词

    	// var_dump($tops);
    	 $sql = "select * from question where ";


    	 if ($tops) {
    	 	foreach ($tops as $row) {
	            $sql .= " title like '%".$row['word']."%' or";

	        }
	        $sql =  rtrim($sql,'or');
    	 }else{
    	 	$sql .= " title like '%".$q."%'";
    	 }

        $res = M()->query($sql);

   		$this->assign('res',$res);
   		//接受return回来的答案后分配到模板
   		 $this->display();

    }
public function all()
{   
    $sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip<>'0'";

        $tips = M()->query($sql);
        $this->assign('tips',$tips);


       $count =  M('question')->where("tip='0'")->count();

       $Page = new \Think\Page($count,6);  //一页显示多少个帖子

       $show = $Page->show();

       $this->assign('page',$show);


       // var_dump($Page->firstRow,$Page->listRows);


        $sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip='0' order by t1.create_time desc limit ".$Page->firstRow.",".$Page->listRows ;

        $questions = M()->query($sql);
        $this->assign('questions',$questions);
        
        $this->assign('title',"首页");
        $this->display();
}

//收藏
     public function storeAdd()
    {
        $data = I('post.');//接数据

        $data['uid']=$_SESSION['id'];       //获取登录用户id

        $data['create_time'] = time();       //追加时间项

        if(M('store')->add($data)){    //将数据放到收藏表中
            $arr = array(
                'error'=>0,
                'info'=>'收藏成功',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'收藏失败'
                );
            echo json_encode($arr);
            exit;
        }
        
    }

//取消收藏
   public function storeDel()
    {
        $qid = I('post.qid');    //接数据
        $uid = $_SESSION['id'];
        $data['uid']=$_SESSION['id'];   //获取登录用户id

        $sql = "delete from store where qid=$qid and uid=$uid";

        $res = M('store')->execute($sql);
        if($res){   //先实例化再执行删除语句
            $arr = array(
                'error'=>0,
                'info'=>'取消收藏',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'取消失败',
                );
            echo json_encode($arr);
            exit;
        }
  
    }

//采纳功能
   public function accept()
    {

        $qid = I('post.qid');
        $aid = I('post.aid');


        $sql="update answer set status = 1 where id = $aid ";
        $res1=M('question')->execute($sql);

        $sql="update question set status = '已采纳' where id = $qid ";

        $res2=M('question')->execute($sql);
        

        if($res1 && $res2){
            $arr = array(
                'error'=>0,
                'info'=>'确定采纳此答案为最佳答案?',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'失败'
                );
            echo json_encode($arr);
            exit;
        }
        
    }

//点赞功能
   public function zan()
    {

        $uid = $_SESSION['id'];
        $aid = I('post.aid');

        var_dump(6666);



        // $sql="update question set status = '已采纳' where id = $qid ";

        // $res2=M('question')->execute($sql);


    }






}